package cn.wolfcode.luowowo.webSite.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.cache.domain.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.common.domain.StrategyComment;
import cn.wolfcode.luowowo.common.query.StrategyCommentObject;
import cn.wolfcode.luowowo.common.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.IStrategyTemplateSearchService;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import cn.wolfcode.luowowo.webSite.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/strategy")
public class StrategyController {

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyTagService strategyTagService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyCommentService strategyCommentService;

    @Reference
    private IStrategyStatisRedisService strategyStatisService;

    @Reference
    private IStrategyCommendService commendService;

    @Reference
    private IStrategyTemplateSearchService strategyTemplateSearchServic;

    @RequestMapping("")
    public String index(Model model) {
        //获取前五个推荐攻略
        List<StrategyCommend> commends = commendService.getTop5();
        model.addAttribute("commends", commends);

        //获取zset中所有的攻略
        List<StrategyStatisVO> list = strategyStatisService.getAllVOByZset();

        //分国内和海外分别获取10个推荐攻略
        List<StrategyStatisVO> abroadCds = new ArrayList<>();
        List<StrategyStatisVO> unabroadCds = new ArrayList<>();
        for (StrategyStatisVO vo : list) {
            if(abroadCds.size()==10 && unabroadCds.size()==10){
                break;
            }
            if(vo.isIsabroad()){
                if(abroadCds.size()<10){
                    abroadCds.add(vo);
                }
            }else {
                if (unabroadCds.size()<10){
                    unabroadCds.add(vo);
                }
            }
        }
        model.addAttribute("abroadCds", abroadCds);
        model.addAttribute("unabroadCds", unabroadCds);

        //热门攻略查询
        List<StrategyStatisVO> hotCds = strategyStatisService.getHotTop10();

        model.addAttribute("hotCds",hotCds );

        //主题推荐: List<Map<String,Object>>
        //外层: list集合
        //内层: 左边:主题(id name)  右边:目的地集合 List集合中目的地: (id name)
        List<Map<String,Object>> data = strategyTemplateSearchServic.getThemeCommend();
        model.addAttribute("themeCds", data);


        //查询条件
        //国内条件:通过省份id和省份名 分  : List<StatisVO>  vo(id,name,count)
        List<StatisVO> chinas = strategyTemplateSearchServic.queryConditionGroup(SearchQueryObject.CONDITION_TYPE_UN_ABROAD);
        model.addAttribute("chinas",chinas);

        //国外条件:通过国家id和国家的名 分
        List<StatisVO> abroads = strategyTemplateSearchServic.queryConditionGroup(SearchQueryObject.CONDITION_TYPE_ABROAD);
        model.addAttribute("abroads",abroads);

        //主题条件: 通过主题id 主题的名 分
        List<StatisVO> themes = strategyTemplateSearchServic.queryConditionGroup(SearchQueryObject.CONDITION_TYPE_THEME);
        model.addAttribute("themes",themes);



        return "/strategy/index";
    }

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQuery qo) {

        //调用之前处理吐司的方法,其实就是一层层查询出父地区
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());

        //把最后一层给剔除掉
        Destination dest = toasts.remove(toasts.size() - 1);

        model.addAttribute("toasts", toasts);

        //把最后一层也返回前端页面处理
        model.addAttribute("dest", dest);

        List<StrategyTag> tags = strategyTagService.list();
        model.addAttribute("tags", tags);

        PageInfo pageInfo = strategyDetailService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        return "/strategy/list";
    }

    @RequestMapping("/detail")
    public String detail(Model model, Long id, @UserParam UserInfo userInfo) {

        //展示明细内容
        StrategyDetail detail = strategyDetailService.get(id);
        StrategyContent content = strategyDetailService.getContent(id);
        detail.setStrategyContent(content);
        model.addAttribute("detail", detail);

        //判断是否登录
        model.addAttribute("log", userInfo);
        if(userInfo==null){
            model.addAttribute("isFavor", false);
        }else {
            boolean ret = strategyStatisService.isFavor(id, userInfo.getId());
            model.addAttribute("isFavor", !ret);
        }

        //阅读量 +1
        strategyStatisService.viewnumIncrease(id, 1);

        //获取vo对象回显页面
        model.addAttribute("vo", strategyStatisService.getStrategyStatisVO(id));

        //热门排行分数加1
        strategyStatisService.addHotScroes(RedisKeys.STRATEGY_STATIS_VO.join(id.toString()),1);

        return "/strategy/detail";
    }

    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(StrategyComment strategyComment, @UserParam UserInfo userInfo) {

        //把用户的数据注入到攻略评论对象里
        strategyComment.setCity(userInfo.getCity());
        strategyComment.setHeadUrl(userInfo.getHeadImgUrl());
        strategyComment.setLevel(userInfo.getLevel());
        strategyComment.setUsername(userInfo.getNickname());
        strategyComment.setUserId(userInfo.getId());

        strategyCommentService.saveOrUpdate(strategyComment);  //保存评论

        //增加评论数
        strategyStatisService.replynumIncrease(strategyComment.getDetailId(), 1);

        //获取当前游记的评论数
        int replynum = strategyStatisService.getStrategyStatisVO(strategyComment.getDetailId()).getReplynum();

        //热门排行分数加1
        strategyStatisService.addHotScroes(RedisKeys.STRATEGY_STATIS_VO.join(strategyComment.getDetailId().toString()),1);

        return AjaxResult.SUCCESS.addData(replynum);
    }

    //评论显示
    @RequestMapping("/comment")
    public String comment(Model model, @ModelAttribute("qo") StrategyCommentObject qo) {
        Page page = strategyCommentService.query(qo);
        model.addAttribute("page", page);
        return "/strategy/commentTpl";
    }

    //攻略评论的点赞
    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toid, Long fromid) {
        strategyCommentService.thumbUp(toid, fromid);

        return AjaxResult.SUCCESS;
    }

    //攻略的收藏
    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long sid, @UserParam UserInfo userInfo) {
        //登录判断
        if (userInfo == null) {
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(102);  //事先约定的未登录状态码
            return result;
        }
        boolean ret = strategyStatisService.favornumIncrease(sid, userInfo.getId());

        //推荐攻略的加分和减分
        //key 分数  这里的value即vo存在Redis里面的key
        strategyStatisService.addCommendScore(RedisKeys.STRATEGY_STATIS_VO.join(sid.toString()), ret ? 1 : -1);

        return new AjaxResult(ret, "").addData(strategyStatisService.getStrategyStatisVO(sid));
    }

    //攻略的顶
    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid, @UserParam UserInfo userInfo) {
        //登录判断
        if (userInfo == null) {
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(102);  //事先约定的未登录状态码
            return result;
        }
        boolean ret = strategyStatisService.thumbsupnumIncrease(sid, userInfo.getId());

        //推荐攻略的加分
        if (ret) {
            strategyStatisService.addCommendScore(RedisKeys.STRATEGY_STATIS_VO.join(sid.toString()), 1);
        }

        return new AjaxResult(ret, "").addData(strategyStatisService.getStrategyStatisVO(sid));
    }

    @RequestMapping("/searchPage")
    public String searchPage(Model model,SearchQueryObject qo) {
        Page page = strategyTemplateSearchServic.query(qo);
        model.addAttribute("page", page);
        return "/strategy/searchPageTpl";
    }
}
